//
//  YPMyInvestItemView.m
//  ChengHuiTong
//
//  Created by CHT-Technology on 2017/3/2.
//  Copyright © 2017年 张文权. All rights reserved.
//

#import "YPMyInvestItemView.h"
#import "YPMyInvestCell.h"

@implementation YPMyInvestItemView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    
    self = [super initWithFrame:frame style:style];
    if(self){
        
        [self setUp];
    }
    return self;
}

#pragma mark -- privateMethod
- (void)setUp{
    
    self.separatorInset = UIEdgeInsetsZero;
    self.delegate = self;
    self.dataSource = self;
    self.tableFooterView = [UIView new];
    
    WeakSelf
    [self addGifHeaderWithRefreshingBlock:^{
        weakSelf.configModel.page = 1;
        [weakSelf setUpData];
    }];
    
    self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.configModel.page ++;
        [weakSelf setUpData];
    }];
    
    self.footer.hidden = YES;
    
}

- (void)setUpData{
    NSLog(@"%@,%ld",self.configModel.title,(long)self.configModel.page);
    NSString *option = @"";
    if ([self.configModel.title isEqualToString:@"回款中"]) {
        option = @"1";
    }else if ([self.configModel.title isEqualToString:@"投标中"]){
        option = @"2";
    }else if ([self.configModel.title isEqualToString:@"已结束"]){
        option = @"3";
    }else if ([self.configModel.title isEqualToString:@"已转让"]){
        option = @"4";
    }
    [YPHttpRequestMethod sendAsyRequestURL:CMD_MYTENDER withData:@{@"token":TOKEN,@"page":@(self.configModel.page),@"option":option} withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        [self.header endRefreshing];
        [self.footer endRefreshing];
        if ([responseDictionary[@"status"] integerValue] == 0) {
            
            [self configModelsWithDataArray:responseDictionary[@"list"]];
            
        }
        [self reloadData];
        
    } withFailedBlock:^(NSError *error) {
        
        [self.header endRefreshing];
        [self.footer endRefreshing];
        [self reloadData];
        
    }withIndicator:NO  atView:self];
}

- (void)configModelsWithDataArray:(NSArray *)dataArray{
    
    if (self.configModel.page == 1) {
        [self.configModel.dataArray removeAllObjects];
    }
    
    for (NSDictionary *dic in dataArray) {
        
        [self.configModel.dataArray addObject:[[YPMyInvestModel alloc]initWithDic:dic]];
    }
    
    if ([dataArray count] < 5) {
        [self.footer noticeNoMoreData];
    }
    
    self.footer.hidden = self.configModel.dataArray.count==0?YES:NO;
    if (self.footer.hidden) {
        self.tableFooterView = [SelectView footViewWithheight:self.height];
    }else{
        self.tableFooterView = [UIView new];
    }
    
}

#pragma mark --UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.configModel.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //根据数据模型来确定不同的重用符即可
    YPMyInvestCell*cell = [tableView dequeueReusableCellWithIdentifier:self.configModel.title];
    if (!cell) {
        cell = [[YPMyInvestCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.configModel.title];
    }
    if (self.configModel.dataArray.count > indexPath.row) {
        cell.model = self.configModel.dataArray[indexPath.row];
    }
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.configModel.title isEqualToString:@"已结束"]) {
        return 148;
    }else{
        return 126;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.cellClickBlock) {
        if (self.configModel.dataArray.count > indexPath.row) {
            YPMyInvestModel *model = self.configModel.dataArray[indexPath.row];
            self.cellClickBlock(model);
        }
    }
}

@end
